<?php
	ob_start();
	error_reporting(E_ALL);
	ini_set('display_errors', 1);
	header('X-UA-Compatible: IE=edge,chrome=1');
	$server = $_SERVER['HTTP_HOST'];
	$path = ($server == 'localhost' ? '/thesett/admin/' : '/');
	include_once 'inc/db.php';
	include_once 'inc/security.php';
	sec_session_start();
	$cn = new db_mysqli();
	if (login_check($cn) == false) header('location: '.LOGIN);
	date_default_timezone_set('Africa/Johannesburg');
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
	<head>
		<meta charset="utf-8">
		<title>My Account. The Sett</title>
		<meta name="description" content="">
		<meta name="keywords" content="">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
		
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta property="og:image" content="img/apple-touch-icon-114x114-precomposed.png"/>
		<link rel="apple-touch-icon-precomposed" href="img/apple-touch-icon-114x114-precomposed.png"/>

		<link rel="stylesheet" type="text/css" href="<?php echo $path; ?>css/wrapper.css">
		<link rel="stylesheet" type="text/css" href="<?php echo $path; ?>css/fullcalendar.css" />
		<link rel="stylesheet" type="text/css" href="<?php echo $path; ?>css/smoothness/jquery-ui-1.10.1.custom.min.css" />
		<link rel="icon" type="image/png" href="<?php echo $path; ?>img/favicon.png">
		<!--<link rel="apple-touch-icon-precomposed" href="apple-touch-icon-114x114-precomposed.png"/>-->
		<script>
			var year = <?php echo date('Y'); ?>;
			var month = <?php echo date('m'); ?>;
			var day = <?php echo date('d'); ?>;
			if (window.location != window.parent.location) {
				top.location = self.location.href;
			}
		</script>
	</head>

<?php
	$user_id	= $_SESSION['user_id'];
	$comp		= $_SESSION['comp'];
	$admin		= $_SESSION['admin'];
	$user_type	= $_SESSION['user_type'];

	$usr_tbl 	= 'users';
	if ($admin == 1) $usr_tbl = 'admins';
	$userdata	= $cn->Data("select * from $usr_tbl where id=$user_id limit 1", false);
	$userdata	= $userdata[0];
	if ($admin == 1) $user_id = 0;

	$tables 	= unserialize(TABLES);
	$titles 	= unserialize(NAVIGATION);
	$icons 		= unserialize(NAVIGATION_ICONS);
	
	$table 		= isset($_GET['tbl']) ? $_GET['tbl'] : $tables[0];
	$realtable 	= $table == 'company_users' && $user_type != 1 ? 'companies' : $table;
	$title 		= $titles[array_search($realtable, $tables)];
	$icon 		= $icons[array_search($realtable, $tables)];
	if ($title[0] == '_') $title = substr($title, 1);
	//$func		= $labels[array_search($table, $tables)];
	$fn 		= isset($_GET['fn']) ? $_GET['fn'] : 'list';
	$post_id	= isset($_GET['id']) ? $_GET['id'] : 0;

	$_SESSION['table'] = $table;
?>
	
	<body>
		<div class="modal">
			<div class="msg">
				<h2>Header</h2>
				<p>This is the message.</p>
			</div>
			<button id="modal-cancel">Cancel</button>
			<button id="modal-confirm">Confirm</button>
		</div>

		<header class="main-header">
			<h2 class="time"></h2>
			<h3 class="date"></h3>
			<div class="menu">
				<nav>
					<div class="welcome">
						<img src="<?php echo $path; ?>img/blank.gif"/>Welcome back, <?php echo $userdata['name']; ?>
					</div>
					<!--<a href="<?php echo $path; ?>settings" class="settings">
						<img src="<?php echo $path; ?>img/blank.gif"/>SETTINGS
					</a>-->
					<?php $item = ($admin == 1 ? 'alerts' : 'messages'); ?>
					<a href="<?php echo $path.$item; ?>" class="<?php echo $item; ?>">
						<img src="<?php echo $path; ?>img/blank.gif"/><?php echo strtoupper($item); ?>
						<span class="count">0</span>
					</a>
					<a href="<?php echo $path; ?>logout" class="logout">
						<img src="<?php echo $path; ?>img/blank.gif"/>LOGOUT
					</a>
				</nav>
			</div>
		</header>
		
		<nav class="main-nav">
		<?php
			switch($user_type) {
				case 0: $type = 'User'; break;
				case 1: $type = 'Company'; break;
				case 2: $type = 'Admin'; break;
			}
		?>
			<h2><?php echo $type; ?> functions</h2>
		<?php
			$type = null;
			for ($i = 0; $i < sizeof($titles); $i++) {
				if ($titles[$i][0] != '_') {
					$class = $realtable == $tables[$i] ? ' class="selected"' : '';
					//if ($class != '') unset($_SESSION['return_path']);
					echo "\t\t\t<a href=\"".$path.$tables[$i]."\"$class><img src=\"".$path."img/blank.gif\" class=\"".$icons[$i]."\" />".$titles[$i]."</a>\n";
				}
			}
		?>
		</nav>
		
		<div class="main-body">
			<article class="wrapper">
				<section>
					<h2><?php echo $title; ?></h2>
					<?php
						if (($fn == 'list' && $table != 'invoices' && $table != 'logs' && $user_type == 2) || ($fn == 'list' && $table == 'bookings' || $table == 'company_users' || $table == 'training_bookings')) 
							echo "<a class=\"add_new\" href=\"".$path.$table."/add\"><img src=\"".$path."img/blank.gif\" alt=\"\" /> Add a new entry</a>";
					?>
					<nav class="breadcrumbs">
					<?php
						if ($fn == 'list' || $fn == 'view') unset($_SESSION['return_path']);
						$return = isset($_SESSION['return_path']) ? $_SESSION['return_path'] : $path.$table;
					?>
						<a href="<?php echo $path.'dashboard'; ?>"><img src="<?php echo $path; ?>img/blank.gif" alt="" /></a>
						<a href="<?php echo $return; ?>"><?php echo $title; ?></a>
					<?php
						if ($fn == 'edit' || $fn == 'view') {
							switch ($table) {
								case 'companies': $fld = 'company'; break;
								case 'company_users':
								case 'users': $fld = 'name'; break;
								case 'bookings': $fld = ''; $post_label = 'Booking'; break;
								case 'alerts': $fld = ''; $post_label = 'Alert'; break;
								default: $fld = 'label';
							}
							$tbl = $table == 'companies' ? 'users' : $table;
							if ($fld != '') {
								$post_label = $cn->Data("select $fld from $tbl where id=$post_id", false);
								$post_label = $post_label[0][$fld];
							}
							echo '<a href="">'.$post_label.' ('.$fn.'ing)</a>';
						} else if ($fn == 'add') {
							echo '<a href="">'.$fn.'</a>';
						}
					?>
					</nav>
				</section>
				<section class="content<?php if ($user_type < 2) echo ' user'; ?>">
					<?php
						if ($table == 'settings') {
							$post_id = $user_id;
							$table = 'users';
							$_SESSION['return_path'] = $path.'settings';
							include 'inc/edit.php';
						} else
							if (file_exists('inc/'.$fn.'.php')) include 'inc/'.$fn.'.php';
					?>
				</section>
				<?php
					if ($fn == 'view' && $children) {
				?>
				<section class="content">
					<?php include 'inc/children.php'; ?>
				</section>
				<?php
					}
				?>
			</article>
		</div>
		<input type="hidden" id="server-path" value="<?php echo $path; ?>" />
		<input type="hidden" id="user-id" value="<?php echo $user_id; ?>" />

		<?php //<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> ?>
		<script>window.jQuery || document.write('<script src="<?php echo $path; ?>js/vendor/jquery-1.9.1.min.js"><\/script>')</script>
		<script type="text/javascript" src="<?php echo $path; ?>js/vendor/jquery-ui-1.10.1.custom.min.js"></script>
		<script type="text/javascript" src="<?php echo $path; ?>js/vendor/globalize.js"></script>
		<script type="text/javascript" src="<?php echo $path; ?>js/vendor/globalize.culture.de-DE.js"></script>
		<script type="text/javascript" src="<?php echo $path; ?>js/vendor/fullcalendar.min.js"></script>
		<script type="text/javascript" src="<?php echo $path; ?>js/vendor/autoNumeric.min.js"></script>
		<script type="text/javascript" src="<?php echo $path; ?>js/vendor/jquery.tablesorter.min.js"></script>
		<script type="text/javascript" src="<?php echo $path; ?>js/wrapper.js"></script>
		<script type="text/javascript">
			var _gaq = _gaq || [];
			_gaq.push(['_setAccount', 'UA-37562452-1']);
			_gaq.push(['_trackPageview']);
			(function() {
			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
			})();
		</script>
	</body>
</html>
<?php ob_flush(); ?>